/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.0
import QtQml 2.12
import org.ukui.tablet 1.0
import QtQuick.Layouts 1.12

Item {
    id: wiressStateRoot
    width: Style.widthScaleFactor * 100; height: Style.heightScaleFactor * 50

    property int networkStrength

    property int wireStateValue

    property bool wiredState

    signal wiressSignal

    RowLayout{
        anchors.fill: parent
        Image {
            id: image
            smooth: true
            Layout.preferredWidth: Style.widthScaleFactor * 32;
            Layout.preferredHeight: Style.heightScaleFactor * 32;
            sourceSize.width: Style.widthScaleFactor * 32;
            sourceSize.height: Style.heightScaleFactor * 32;
        }
    }

    //无线网络状态改变
    function wiredStateChange(wireStrength){
        if(wiredState == false && wireStateValue != 4){
            networkStrength = wireStrength
            if(networkStrength <= 75 && networkStrength > 55 ){
                image.source = "qrc:/ui/images/network-wireless-signal-good-symbolic.svg"
            }
            else if(networkStrength <= 55 && networkStrength > 35){
                image.source = "qrc:/ui/images/network-wireless-signal-ok-symbolic.svg"
            }
            else if(networkStrength <= 35 && networkStrength > 15){
                image.source = "qrc:/ui/images/network-wireless-signal-weak-symbolic.svg"
            }
            else if(networkStrength > 75){
                image.source = "qrc:/ui/images/network-wireless-signal-excellent-symbolic.svg"
            }
            else if((networkStrength == -1 || networkStrength <= 15) && wiredLinkState.wiredState == false){
                image.source = "qrc:/ui/images/network-disconnected-dark.svg"
            }
        }
    }

    //有线网络状态的改变
    function wiredLinkChange(linkStated){
        wiredState = linkStated
        wireStateValue = wiredLinkState.wiredStateValue
        if(wiredState == false && (networkStrength == -1 || networkStrength <= 15)){
            image.source = "qrc:/ui/images/network-disconnected-dark.svg"
        }
        else{
            if(wiredState == true){
                if(wireStateValue == 4){
                    image.source = "qrc:/ui/images/network-connected-dark.svg"
                }
                else{
                    image.source = "qrc:/ui/images/network-error-dark.svg"
                }
            }
        }
    }

    WirelessStatus {
        id: wiressState
    }

    WiredLinkState{
        id: wiredLinkState

        onWiredLinkChanged: {
            wiredLinkState.wiredLinkChanged
        }
    }

    Component.onCompleted:{
        //无线链接
        wiressStateRoot.wiredStateChange(wiressState.wiredState)
        wiressState.wiredStateChanged.connect(wiressStateRoot.wiredStateChange)

        //有线链接
        wiressStateRoot.wiredLinkChange(wiredLinkState.wiredState)
        wiredLinkState.onWiredLinkChanged.connect(wiressStateRoot.wiredLinkChange)
    }
}
